package com.alogic.poi.xscript;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;

import com.alogic.xscript.AbstractLogiclet;
import com.alogic.xscript.ExecuteWatcher;
import com.alogic.xscript.Logiclet;
import com.alogic.xscript.LogicletContext;
import com.alogic.xscript.doc.XsObject;
import com.anysoft.util.BaseException;
import com.anysoft.util.IOTools;
import com.anysoft.util.Properties;
import com.anysoft.util.PropertiesConstants;
import com.anysoft.util.Settings;
import com.logicbus.backend.Context;
import com.logicbus.backend.server.http.HttpCacheTool;

/**
 * 作为服务数据写出
 * 
 * @author yyduan
 * @since 1.6.11.35
 * 
 * @version 1.6.12.5 [20181101 duanyy] <br>
 * - 输出时,可指定文件名 <br>
 */
public class XsAsServant extends AbstractLogiclet{
	protected String pid = "$workbook";
	protected String contextId = "$context";
	protected String $contentType = "application/vnd.ms-excel";
	protected String $cacheEnable = "true";
	protected String $filename = "";
	protected String encoding = "utf-8";
	protected HttpCacheTool cacheTool = null;
	
	public XsAsServant(String tag, Logiclet p) {
		super(tag, p);
	}

	@Override
	public void configure(Properties p){
		super.configure(p);
		pid = PropertiesConstants.getString(p,"pid",pid);
		contextId = PropertiesConstants.getString(p,"contextId",contextId);
		$contentType = PropertiesConstants.getRaw(p,"contentType",$contentType);
		$cacheEnable = PropertiesConstants.getRaw(p, "cacheEnable", $cacheEnable);
		$filename = PropertiesConstants.getRaw(p, "filename", $filename);		
		cacheTool = Settings.getToolkit(HttpCacheTool.class);
		encoding = PropertiesConstants.getString(p,"encoding",encoding);		
	}
	
	@Override
	protected void onExecute(XsObject root,XsObject current, LogicletContext ctx, ExecuteWatcher watcher) {
		Workbook workbook = ctx.getObject(pid);
		if (workbook == null){
			throw new BaseException("core.e1001","It must be in a workbook context,check your together script.");
		}

		Context serviceContext = ctx.getObject(contextId);
		if (serviceContext == null){
			throw new BaseException("core.e1001","It must be in a DownloadTogetherServant servant,check your together script.");
		}
		
		OutputStream output = null;
		try {
			if (PropertiesConstants.transform(ctx, $cacheEnable, true)){
				cacheTool.cacheEnable(serviceContext);
			}else{
				cacheTool.cacheDisable(serviceContext);			
			}
			
			String filename = PropertiesConstants.transform(ctx, $filename,"");
			if (StringUtils.isNotEmpty(filename)){
				filename = URLEncoder.encode(filename, encoding);
				serviceContext.setResponseHeader("Content-Disposition", 
					String.format("attachment; filename=%s;filename*=%s''%s",filename,encoding,filename));
			}			
			serviceContext.setResponseContentType(PropertiesConstants.transform(ctx, $contentType, "application/vnd.ms-excel"));			
			
			output = serviceContext.getOutputStream();
			workbook.write(output);
		} catch (IOException e) {
			throw new BaseException("core.e1004","Can not write excel file.");
		}finally{
			IOTools.close(output);
		}
	}	
}

